home *** CD-ROM | disk | FTP | other *** search
/ Nebula 2 / Nebula Two.iso / Apps / DevTools / eText5 / Source / unused4Code / BBC.bproj / DocumentIcon.m < prev    next >
Encoding:
Text File  |  1977-07-02  |  5.4 KB  |  231 lines

  1. #import <dbkit/dbkit.h>
  2. #import "DocumentIcon.h"
  3.  
  4. @implementation DocumentIcon
  5. - initFrame:(const NXRect *)frameRect
  6. {
  7.     const char *dragTypes;
  8.     [super initFrame:frameRect];
  9.     dragTypes = NXFilenamePboardType;
  10.     [self registerForDraggedTypes:&dragTypes count: 1];
  11.     return self;
  12. }
  13.  
  14. - free
  15. {
  16.     [self unregisterDraggedTypes];
  17.     return [super free];
  18. }
  19.  
  20.  
  21. - drawSelf:(const NXRect *)rects :(int)rectCount
  22. {
  23.     NXRect imageRect;
  24.     NXSize imageSize;
  25.     NXPoint drawOrigin;
  26.     
  27.     NXDrawGrayBezel(&bounds, rects);
  28.     imageRect.origin.x    = bounds.origin.x    + 2;
  29.     imageRect.origin.y    = bounds.origin.y    + 2;
  30.     imageRect.size.width  = bounds.size.width  - 4;
  31.     imageRect.size.height = bounds.size.height - 4;
  32.  
  33.     PSgsave();
  34.     NXRectClip(&imageRect);
  35.  
  36.     [image getSize:&imageSize];
  37.     drawOrigin    = imageRect.origin;
  38.     drawOrigin.x += (imageRect.size.width  - imageSize.width)  / 2;
  39.     drawOrigin.y += (imageRect.size.height - imageSize.height) / 2;
  40.     [image composite:NX_SOVER toPoint:&drawOrigin];
  41.  
  42.     PSgrestore();
  43.  
  44.     return self;
  45. }
  46.  
  47. #define mask (NX_LMOUSEUPMASK|NX_LMOUSEDRAGGEDMASK)
  48. #define Shift(e) (e->flags&(NX_NEXTLSHIFTKEYMASK|NX_NEXTRSHIFTKEYMASK))
  49. #ifndef abs
  50. #define abs(x) (((x)<0)? -(x) : (x))
  51. #endif
  52.  
  53. int
  54. mouseMoved(NXPoint *o, int n) { /* true if mouse moves > n pixels from 'o' */
  55.     NXEvent *e;
  56.     NXPoint p;
  57.     float d;
  58.     do {
  59.         e = [NXApp getNextEvent:mask];
  60.         p = e->location;
  61.         d = abs(p.x-o->x);
  62.         if (d < abs(p.y-o->y)) d = abs(p.y-o->y);
  63.     } while (e->type != NX_LMOUSEUP && d<n);
  64.     *o = p;
  65.     return e->type != NX_LMOUSEUP;
  66. }
  67.  
  68. - mouseDown: (NXEvent *) e
  69. {
  70.     NXPoint    hitPoint;
  71.     int        oldMask;
  72.     NXEvent    saveEvent;
  73.     Pasteboard    *dragPasteboard;
  74.  
  75.  
  76.     if( e->data.mouse.click > 1
  77.         && filename && *filename) {
  78.     [[Application workspace] openFile: filename];
  79.     }
  80.  
  81.     oldMask = [window eventMask];
  82.     [window setEventMask:(oldMask|mask)];
  83.     saveEvent = *e;
  84.     hitPoint = e->location;
  85.  
  86.     if (mouseMoved(&hitPoint,4)) {
  87.     NXRect    dragRect;
  88.     
  89.     if( filename && *filename) {
  90.         [self convertPoint: &hitPoint fromView: nil];
  91.         [self getBounds: &dragRect];
  92.  
  93. //        hitPoint.x -= dragRect.origin.x;
  94. //        hitPoint.y -= dragRect.origin.y;
  95.         
  96.         dragPasteboard = [Pasteboard newName: NXDragPboard];
  97.         [dragPasteboard declareTypes: &NXFilenamePboardType num: 1 owner: nil];
  98.         [dragPasteboard writeType: NXFilenamePboard data: filename
  99.             length: strlen( filename)];
  100.         
  101.         [self dragImage:image at:&(dragRect.origin) offset:&hitPoint
  102.             event:&saveEvent pasteboard:dragPasteboard
  103.             source:self slideBack: YES];
  104.     }
  105.     }
  106.  
  107.     return [super mouseDown: e];
  108. }
  109.  
  110. - (NXDragOperation)draggingSourceOperationMaskForLocal:(BOOL)flag
  111. {
  112.     return NX_DragOperationCopy;
  113. }
  114.  
  115. - setTarget:anObject            {target = anObject; return self;}
  116. - setAction:(SEL)aSelector        {action = aSelector; return self;}
  117. - target                {return target;}
  118. - (SEL)action                {return action;}
  119.  
  120. - (const char *) stringValue
  121. {
  122.     return filename;
  123. }
  124.  
  125. - setStringValue: (const char *) aString
  126. {
  127.     if( filename) NXZoneFree( [self zone], filename), filename = NULL;
  128.     filename = NXCopyStringBufferFromZone( aString, [self zone]);
  129.     if( filename && *filename) {
  130.     image = [[Application workspace] getIconForFile: filename];
  131.     [self display];
  132.     }
  133.     return self;
  134. }
  135.  
  136.  
  137. /**********************************************************************/
  138. /*                            Archiving                               */
  139.  
  140. - read:(NXTypedStream *)stream
  141. {
  142.     const char *dragTypes = NXFilenamePboardType;
  143.     [super read: stream];
  144.     [self registerForDraggedTypes:&dragTypes count: 1];
  145.     return self;
  146. }
  147.  
  148. /**********************************************************************/
  149. /*                            Dragging                                */
  150.  
  151. - (NXDragOperation) draggingEntered: sender
  152. {
  153.     id dragPasteboard;
  154.     char *path;
  155.     int length;
  156.  
  157.     dragPasteboard = [sender draggingPasteboard];
  158.  
  159.     if( ([dragPasteboard readType:NXFilenamePboardType
  160.         data:&path length:&length])
  161.         && (path && *path && *path == '/')) {
  162.     return  NX_DragOperationCopy;
  163.     }
  164.  
  165.     return NX_DragOperationNone;
  166. }
  167.  
  168.  
  169.  
  170. - (BOOL)performDragOperation:(id <NXDraggingInfo>)sender
  171. {
  172.     char *path;
  173.     int length;
  174.     id dragPasteboard, newImage = nil;
  175.  
  176.     dragPasteboard = [sender draggingPasteboard];
  177.  
  178.     if ([dragPasteboard readType:NXFilenamePboardType
  179.         data:&path length:&length]) {
  180.  
  181.     char p[MAXPATHLEN+1];
  182.     strncpy( p, path, length);
  183.     p[length] = '\0';
  184.     if( filename) NXZoneFree( [self zone], filename), filename = NULL;
  185.     filename = NXCopyStringBuffer(p);
  186.     image = [sender draggedImageCopy];
  187.     [dragPasteboard deallocatePasteboardData:path length:length];
  188.     
  189.     [self display];
  190.     if( target != nil)
  191.         [self sendAction:action to:target];
  192.         
  193.     return YES;
  194.     }
  195.  
  196.     return NO;
  197. }
  198.  
  199.  
  200. - draggingExited:sender
  201. {
  202.     return self;
  203. }
  204.  
  205. /**********************************************************************/
  206. /*                 DBCustomAssociation Implementation                 */
  207. - _updateForAssociation: association
  208. {
  209.     id val = [[DBValue alloc] init];
  210.     [[[association fetchGroup] recordList] getValue: val
  211.     forProperty: [association expression]
  212.         at: [[association fetchGroup] currentRecord]];
  213.     [self setStringValue: [val stringValue]];
  214.     [val free];
  215.     return self;
  216. }
  217.  
  218. - associationContentsDidChange:association
  219. {
  220.     return [self _updateForAssociation: association];
  221. }
  222.  
  223. - associationSelectionDidChange:association
  224. {
  225.     return [self _updateForAssociation: association];
  226. }
  227.  
  228. @end
  229.  
  230.  
  231.